<?php
namespace App\Modules\Relationship\Models;

use App\Modules\Relationship\Interfaces\ModelInterface;
use App\Http\Services\CommonServices;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Http\Model\Base;

class SweetFriendModel extends Base implements ModelInterface {

    protected $table = "rs_sweet_friend";
    protected $titleTable = "rs_sweet_user_title";
    protected $fields = ['id', 'user_id as userId', 'friend_id as friendId',
        'intimate', 'title_id as titleId',
        'level', 'created_time as createdTime'];
    protected $tablePrefix = '';

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
        $this->tablePrefix = env('DB_PREFIX');
    }

    // 获取某条用户关系记录
    public function getRecord(int $userId, int $friendId){
        $condition = array(
            "user_id"=>$userId,
            "friend_id"=>$friendId,
            "is_deleted"=>0
        );
        $return = $this->getOneByCondition($condition, $this->fields);
        return $return;
    }

    // 查询一组记录
    public function getGroupRecord ($userId, $friendId) {
        $re1 = $this->getRecord($userId, $friendId);
        $re2 = $this->getRecord($friendId, $userId);
        if (!$re1 || !$re2) {
            return null;
        }
        return [$re1, $re2];
    }


    public function createRecord (array $data){
        return DB::table($this->table)->insertGetId($data);
    }

    public function updateRecord (array $ids, array $data){
        return DB::table($this->table)->whereIn('id', $ids)->update($data);
    }

    public function deleteRecord (array $ids) {
        return DB::table($this->table)->whereIn('id',  $ids)->update(array('is_deleted'=>1));
    }

    public function addIntimate (int $id, int $intimate) {
        return DB::table($this->table)->where('id', $id)->increment('intimate', $intimate);
    }


    // 获取某条用户关系记录
    public function getTwoRecord(int $userId, int $friendId){
        $condition1 = array(
            "user_id"=>$userId,
            "friend_id"=>$friendId,
            "is_deleted"=>0
        );
        $re1 = $this->getOneByCondition($condition1, $this->fields);
        $condition2 = array(
            "user_id"=>$userId,
            "friend_id"=>$friendId,
            "is_deleted"=>0
        );
        if (!$re1) {
            throw new \Exception("密友记录不存在");
        }
        $re2 = $this->getOneByCondition($condition2, $this->fields);
        if ($re1 && $re2) {
            $re1['friendTitleId'] = $re2['titleId'];
        }

        return $re1;
    }

    // 获取用户关系分表和关联信息表
    public function getRecordAndAttr (string $rsCode) : array {
        $sql = 'SELECT 
        s.intimacy, 
        s.level, 
        s.created_time AS createdTime,
        u.user_id AS userId,
        u.title_id AS titleId 
        FROM `%s` AS s 
        LEFT JOIN `%s` AS u 
        ON s.id = u.sweet_id 
        WHERE  s.is_deleted = 0 
        AND u.is_deleted = 0 
        LIMIT 2';
        $bindings = array(
            $this->tablePrefix . $this->table,
            $this->tablePrefix . $this->titleTable,
            $rsCode
        );
        $data = DB::select($sql, $bindings);
        return $data;
    }

    // 获取某用户的全部密友记录
    public function getAllSweet(int $userId) : array {
        $condition = array(
            "user_id"=>$userId,
            "is_deleted"=>0
        );
        $return = $this->getListByCondition($condition, $this->fields, array('field'=>'intimate', 'sort'=>'desc'));
        if (!$return) {
            return array();
        }
        return $return;
    }

    // 获取用户的密友数量
    public function getSweetFriendsNum (int $userId):int  {
        $condition = array(
            "user_id"=>$userId,
            "is_deleted"=>0
        );
        $return = $this->getListByCondition($condition, $this->fields);
        if (!$return) {
            return 0;
        }
        return count($return);
    }

    // 写入两条记录
    public function insert ($userId, $userTitleId, $friendId, $friendTitleId) {
        $table = $this->tablePrefix . $this->table;
        $sql = "INSERT INTO {$table} (`user_id`, `friend_id`, `title_id`, `level`) value ({$userId}, {$friendId}, {$userTitleId}, 1),({$friendId},{$userId},{$friendTitleId},1)";
        $re = DB::insert($sql);
        return $re;
    }

    public function updateTitle ($userId, $userTitleId, $friendId, $friendTitleId) {
        $re = $this->getGroupRecord($userId, $friendId);

        DB::table($this->table)->where('id', $re[0]['id'])->update(['title_id'=>$userTitleId]);
        DB::table($this->table)->where('id', $re[1]['id'])->update(['title_id'=>$friendTitleId]);
    }
}

